<template>
  <div>
    <el-dialog
      title="绑定设备"
      :visible.sync="dialogVisible"
      width="450px"
      :before-close="closeDialog"
      :close-on-click-modal="false"
    >
      <el-form :model="addForm" :rules="rules" ref="refForm" label-width="95px">

        <el-form-item size="small" label="安装信息" >
          <span class="div_box">
            {{ result.buildingName+'-'+ result.floorName+'-'+ result.name}}
          </span>
        </el-form-item>

        <el-form-item size="small" label="设备编码" prop="imei">
          <el-input
             oninput="value=value.replace(/[\u4e00-\u9fa5]/g,'')"
            @blur="addForm.imei = $event.target.value"
            clearable
            v-model="addForm.imei"
            placeholder="请输入"
            maxlength="25"
          ></el-input>
        </el-form-item>

        <el-form-item size="small" label="位置标记">
          <div style="display: flex; align-items: center">
            <el-button
              size="small"
              type="primary"
              @click="sign"
              icon="el-icon-location-outline"
              >{{ nameMark }}</el-button
            >
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog" class="cancel-button">取 消</el-button>
        <el-button
          size="small"
          @click="submit"
          v-preventReClick="1000" class="confirm-button"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <Ichnography ref="refIchnography" @clickMark="clickMark"></Ichnography>
  </div>
</template>

<script>
import { reqDeviceBind, reqFloorById } from "@api/api.js";
import { validateImei } from "@utils/validate";
import { mapState } from "vuex";
import Ichnography from "../components/Ichnography.vue";

export default {
  props: ["result"],
  components: {
    Ichnography,
  },
  data() {
    return {
      dialogVisible: false,
      buildList: [],
      floorList: [],
      spaceList: [],
      addForm: {
        imei: "",
        buildId: "",
        floorId: "",
        spaceId: "",
      },
      rules: {
        imei: [
          { required: true, message: "请输入设备编码", trigger: "blur" },
          { validator: validateImei },
        ],
        buildId: [
          { required: true, message: "请选择建筑物", trigger: "change" },
        ],
        floorId: [{ required: true, message: "请选择楼层", trigger: "change" }],
        spaceId: [
          { required: true, message: "请选择安装点", trigger: "change" },
        ],
      },
      nameMark: "标记",
      mark: "",
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  mounted() {},
  methods: {
    // 平面图标记
    async sign() {
      const res = await reqFloorById(this.result.id);
      // console.log(res);
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      if (res.data.rows.image == "")
        return this.$modal.notifyWarning("当前楼层暂无平面图,请上传后操作!");
      this.$refs.refIchnography.dialogVisible = true;
      this.$refs.refIchnography.getimage(res.data.rows.image);
      if (this.nameMark == "已标记") {
        this.$refs.refIchnography.getMark(this.mark);
      }
    },
    // 子组件事件
    clickMark(e) {
      // console.log(e);
      this.mark = e;
      this.nameMark = "已标记";
    },
    // 点击绑定
    submit() {
      this.$refs.refForm.validate(async (valid) => {
        if (!valid) return 
        this.$notify.closeAll();
        let { imei } = this.addForm;
        const res = await reqDeviceBind({
          imei,
          spaceId: this.result.id,
          userId: this.user.userId,
          mark: this.mark,
        });
        if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
        this.$modal.notifySuccess("绑定成功");
        this.closeDialog();
        this.$parent.DeviceBySpaceId();
      });
    },
    // 点击关闭弹框
    closeDialog() {
      this.$refs.refForm.resetFields();
      this.$refs.refIchnography.closeDialog();
      this.dialogVisible = false;
      this.nameMark = "标记";
      this.mark = "";
    },
  },
  deactivated() {
    if(this.$refs.refForm!=null){
      this.closeDialog();
    }
  },
};
</script>

<style lang="less" scoped>
.div_box{
  color: @primaryThemeTxtColor2;
}
</style>
